// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        /*cd:{
            type:cc.Node,
            default:null,
        },*/
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        //this.node.on(cc.Node.EventType.TOUCH_MOVE,this.onTouchMove,this);
        this.node.on(cc.Node.EventType.MOUSE_UP, this.onMouseUp, this);
    },

    start () {

    },
    /*onTouchMove:function(event){
        //console.log('鼠标移动了');
        var mousePoint = cc.v2(event.getLocationX(),event.getLocationY());
        var pos = this.node.parent.convertToNodeSpaceAR(mousePoint);
        this.node.x=pos.x;
        this.node.y=pos.y;


    },*/
    onMouseUp: function(event) {
        let mouseType = event.getButton();
        if (mouseType === cc.Event.EventMouse.BUTTON_LEFT) {
            // 鼠标左键释放
            //console.log(this);
            this.node.cd.active = true; //cd效果开启
            this.node.name="planted";
            var plant_points=this.node.parent.getChildByName("plant_points").children;
            for (var i = 0; i < plant_points.length; i++) {
                if(plant_points[i].getBoundingBoxToWorld().contains(event.getLocation())){
                    this.node.position=plant_points[i].position;
                    console.log(i);
                    break;
                }
            }
            //console.log(this.node.parent.getChildByName("plant_points").children[0].getBoundingBoxToWorld());
            /*let mousePoint = event.getLocation();
            let localPoint = this.node.convertToNodeSpace(mousePoint);*/
        }
    },

    // update (dt) {},
});
